/*
 *  Copyright 2020-2021 Li Fei
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.pengshun.modules.api.coal.service.impl;

import com.pengshun.modules.api.coal.para.MerchantPara;
import com.pengshun.modules.api.coal.repository.UserRepository;
import com.pengshun.modules.api.mapper.PsMerchantMapper;
import com.pengshun.modules.api.coal.para.CoalBinessPara;
import com.pengshun.modules.api.coal.para.MapPara;
import com.pengshun.modules.api.coal.result.MerchantMapResult;
import com.pengshun.modules.api.coal.result.MerchantResult;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.domain.Merchant;
import com.pengshun.modules.domain.User;
import lombok.RequiredArgsConstructor;
import com.pengshun.modules.api.coal.repository.MerchantRepository;
import com.pengshun.modules.api.coal.service.MerchantService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ArrayList;
import java.util.Locale;

/**
 * @author lifei
 * @date 2020-11-05
 **/
@Service
@RequiredArgsConstructor
public class MerchantServiceImpl implements MerchantService {

    private final MerchantRepository merchantRepository;

    private final  PsMerchantMapper pmm;

    private final UserRepository userRepository;

    private final MessageSource messageSource;

    @Override
    public BaseResult selectListByMerchantId(MerchantPara para) throws Exception {
        List<MerchantResult> castEntity = pmm.selectListByMerchantId(para.getMerchantCategoryId(), para.getLngX(), para.getLatY());
        return new BaseResult(0, castEntity);
    }

    @Override
    public BaseResult selectListByMineMouthId(MerchantPara para) throws Exception {
        List<MerchantResult> castEntity = pmm.selectListByMineMouthId(para.getMineMouthId(), para.getMerchantCategoryId(), para.getPage(), para.getSize());
        return new BaseResult(0, castEntity);
    }

    @Override
    public BaseResult selectListBy(CoalBinessPara para) {
        int page = (para.getPage()) * para.getSize();
        List<MerchantResult> list = pmm.selectListBy(para.getMerchantCategoryId(), page < 0 ? 0 : page, para.getSize(), para.getCategory(),
                para.getName(), para.getStatus(), para.getSort(), para.getDistrictId(), para.getLevel(), para.getLngX(), para.getLatY(),
                para.getMineMouthId(),para.getCategoryCode());

        return new BaseResult(0, list);
    }

    @Override
    public BaseResult selectAllMerchantMap(MapPara para) {
        List<MerchantMapResult> list = new ArrayList<>();
        if (para.getDistance() == null) {
            list = pmm.selectAllMerchantMap(para.getLongitude(), para.getLatitude(), para.getMerchantCategoryId(), para.getName());
        } else {
            list = pmm.selectAllMerchantMapBy(para.getLongitude(), para.getLatitude(), para.getMerchantCategoryId(), para.getName(), para.getDistance());
        }

        return new BaseResult(0, list);
    }

    @Override
    public BaseResult selectMerchantDetil(Long merchantId) {
        return new BaseResult(0, pmm.selectMerchantDetil(merchantId));
    }

    @Override
    public BaseResult selectMerchant(String phone) {
        User user = userRepository.findByPhone(phone);
        if (user == null) {
            return new BaseResult(1, messageSource.getMessage("MerchantServiceImpl.selectMerchant.user", null, Locale.CHINA));
        }
        if (user.getUserType() != 3) {
            return new BaseResult(1, messageSource.getMessage("MerchantServiceImpl.selectMerchant.getUserType", null, Locale.CHINA));
        }
        if (user.getStatus() != 2) {
            return new BaseResult(1, messageSource.getMessage("MerchantServiceImpl.selectMerchant.getStatus", null, Locale.CHINA));
        }
        Merchant merchant = merchantRepository.findByUserId(user.getId());
        if (merchant == null) {
            return new BaseResult(1, messageSource.getMessage("MerchantServiceImpl.selectMerchant.merchant", null, Locale.CHINA));
        }
        return new BaseResult(0, merchant);
    }
}